package com.shekhar.paas.story;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import com.shekhar.paas.domain.Story;
import com.shekhar.paas.repository.StoryRepository;
import com.shekhar.paas.util.DateUtils;

@Component
public class StoryServiceImpl implements StoryService {

    private static final int TEN = 10;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StoryRepository storyRepository;

    /*
     * (non-Javadoc)
     * 
     * @see com.shekhar.paas.story.StoryService#findCurrentTopTenStories()
     */
    @Override
    public List<Story> findCurrentTopTenStories() {
        Date startDate = DateUtils.getDate(0, 0, 0, 0);
        Date endDate = new Date();
        Sort sort = new Sort(new org.springframework.data.domain.Sort.Order(Direction.DESC, "score"));
        PageRequest pageRequest = new PageRequest(0, TEN, sort);
        Page<Story> storyPage = storyRepository.findByTweets_IsoLanguageCodeAndCreationDateBetween("en", startDate, endDate, pageRequest);
        return storyPage.getContent();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.shekhar.paas.story.StoryService#fetchTopTenForLast24Hours()
     */
    @Override
    public List<Story> fetchTopTenForLast24Hours() {
        Date startDate = org.apache.commons.lang3.time.DateUtils.addDays(new Date(), -1);
        Date endDate = new Date();

        Sort sort = new Sort(new org.springframework.data.domain.Sort.Order(Direction.DESC, "score"));
        PageRequest pageRequest = new PageRequest(0, TEN, sort);
        Page<Story> storyPage = storyRepository.findByTweets_IsoLanguageCodeAndCreationDateBetween("en", startDate, endDate, pageRequest);
        return storyPage.getContent();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.shekhar.paas.story.StoryService#fetchTopTenStories(java.lang.String)
     */
    @Override
    public List<Story> findTopTenStoriesByTag(String tag) {
        Date startDate = DateUtils.getDate(0, 0, 0, 0);
        Date endDate = new Date();
        return findStoriesByTagAndDate(tag, startDate, endDate);
    }

    public List<Story> findTopTenStoriesByTagInLast24Hours(String tag) {
        Date startDate = org.apache.commons.lang3.time.DateUtils.addDays(new Date(), -1);
        Date endDate = new Date();
        return findStoriesByTagAndDate(tag, startDate, endDate);
    }
    
    private List<Story> findStoriesByTagAndDate(String tag, Date startDate, Date endDate) {
        Sort sort = new Sort(new org.springframework.data.domain.Sort.Order(Direction.DESC, "score"));
        PageRequest pageRequest = new PageRequest(0, 10, sort);
        Page<Story> storyPage = storyRepository
                .findByTweets_IsoLanguageCodeAndTagsAndCreationDateBetween("en", tag, startDate, endDate, pageRequest);
        return storyPage.getContent();
    }
    

    /*
     * (non-Javadoc)
     * 
     * @see com.shekhar.paas.story.StoryService#findAllStoriesByDateDesc(int,
     * int)
     */
    @Override
    public List<Story> findAllStoriesByDateDesc(int firstResult, int sizeNo) {
        Query query = new Query(Criteria.where("tweets.isoLanguageCode").is("en")).skip(firstResult).limit(sizeNo);
        query.sort().on("creationDate", Order.DESCENDING);
        return mongoTemplate.find(query, Story.class);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.shekhar.paas.story.StoryService#countAllStories()
     */
    @Override
    public float countAllStories() {
        return mongoTemplate.count(new Query(Criteria.where("tweets.isoLanguageCode").is("en")), Story.class);
    }

    private static Date getEndDate() {
        return new Date();
    }

}
